<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/6/27 9:53
 * Modified Time: 2019/6/27 9:53
 * Version: 2.0
 */

namespace App\Modules\Service\Controllers;


use App\Lib\Utilities;
use App\Modules\Service\Validators\ServiceControllerValidator;
use App\Modules\System\Services\ConfigService;
use Flower\Core\Http\BaseController;
use App\Modules\Service\Services\ServiceService;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;

class ServiceController extends BaseController
{
    private $serviceService;
    private $configService;

    public function __construct(ServiceService $serviceService,
                                ConfigService $configService)
    {
        $this->serviceService = $serviceService;
        $this->configService = $configService;
    }

    public function getRules()
    {
        return ServiceControllerValidator::validator();
    }

    /**
     * 创建服务
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createService(Request $request)
    {
        $store_id = $request->input('store_id');
        $name = $request->input('name');
        $price = $request->input('price');
        $type = $request->input('type');
        $royalty = $request->input('royalty',0);
        $cost = $request->input('cost',0);
        $assistant_royalty = $request->input('assistant_royalty',0);

        $service = $this->serviceService->createService($store_id, $name, $price, $type, $royalty, $cost,$assistant_royalty);

        $data = [
            'service_id' => $service->id
        ];

        return $this->display($data, '添加成功');

    }

    /**
     * 获取本店所有服务
     * @param Request $request
     * @return mixed
     */
    public function getServiceList(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);

        $service = $this->serviceService->getServiceList($store_id, $pageSize);

        return $this->display($service, '数据返回成功');

    }

    /**
     * 获取本店服务列表分页
     * @param Request $request
     * @return mixed
     */
    public function getServiceListPage(Request $request)
    {
        $store_id = $request->input('store_id');
        $service_type = $request->input('service_type');
        $pageSize = $request->input('pageSize', 10);
        $service = $this->serviceService->getServiceListPage($store_id, $service_type, $pageSize);

        return $this->display($service, '数据返回成功');
    }

    /**
     * 获取服务分类
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getServiceType()
    {
        $data = $this->configService->getDictionary('service_type');
        return $this->display($data, '查询成功');
    }

    /**
     * 删除服务
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function delService(Request $request)
    {
        $service_id = $request->input('id');
        $this->serviceService->delService($service_id);
        return $this->success('删除成功');

    }

	/**
	 * 编辑服务
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function editService(Request $request)
    {
        $store_id = $request->input('store_id');
        $service_id = $request->input('id');
        $name = $request->input('name');
        $price = $request->input('price');
        $type = $request->input('type');
        $royalty = $request->input('royalty');
        $cost = $request->input('cost');
	    $assistant_royalty = $request->input('assistant_royalty',0);
        $this->serviceService->editService($store_id, $service_id, $name, $price, $type, $royalty ,$cost,$assistant_royalty);


        return $this->success('编辑成功');

    }

    /**
     * 判断服务是否下架
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\NotFoundException
     */
    public function isOff(Request $request)
    {
        $service_id = $request->input('service_id');
        $res = $this->serviceService->isOff($service_id);
        return $this->display($res);
    }

    public function getServiceTypeAndService(Request $request)
    {
        $store_id = $request->input('store_id');
        $barber_id = $request->input('user_id');
        $data = $this->serviceService->getServiceTypeAndService($store_id,$barber_id);
        return $this->display($data, '查询成功');
    }

}